//
//  LXPaymentRecordViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/27.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXPaymentRecordViewController.h"
#import "LXYearCheckTableViewCell.h"
@interface LXPaymentRecordViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger pageIndex;
@end

@implementation LXPaymentRecordViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    
    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO, kRefreshStr);
        [self.tableView.mj_header beginRefreshing];
    }
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    
    [self layoutCustomNav:self.view title:@"收/付款记录"];
    
    if (self.type==1) {
//        UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 70, KStatuesBarHeight, 60, 40)];
//       rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
//       [rightButton setTitle:@"新增" forState:UIControlStateNormal];
//        [rightButton setImage:Image(@"icon_add") forState:UIControlStateNormal];
//       [rightButton setTitleColor:mainColor forState:UIControlStateNormal];
//        [rightButton addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
//       [self.view addSubview:rightButton];
    }
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXYearCheckTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXYearCheckTableViewCell class])];
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self loadData];
    }];

    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.pageIndex++;
        [self loadData];
    }];
    
  

   
    
}

- (void)loadData{


    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:@(self.pageIndex) forKey:@"page"];
    [dic setValue:@(20) forKey:@"rows"];
    
    if (!StringIsEmpty(self.carModel.ID)) {
        [dic setValue:self.carModel.ID forKey:@"carId"];
    }
    if (!StringIsEmpty(self.customerId)) {
        [dic setValue:self.customerId forKey:@"customerId"];
    }
    
    [MSNetworkManager getRevertUrl:GET_SignpayrecordPage params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        if (isSuccess) {
            if (self.pageIndex==1) {
                [self.data removeAllObjects];
            }
            NSInteger totalPage = [resonseObj[@"total"] integerValue];
                
            NSArray *records = resonseObj[@"records"];
            
            [self.data addObjectsFromArray:[CheLiangModel  mj_objectArrayWithKeyValuesArray:records]];
            
            
            if (self.data.count>=totalPage) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                self.tableView.mj_footer.state = MJRefreshStateIdle;
            }
            
                
            [self.tableView reloadData];
        }
        
    
      
            
        [self isHaveEmptyData];
            
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];

}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    LXYearCheckTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXYearCheckTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];
    CheLiangModel *model = self.data[indexPath.row];
    cell.sfkModel = model;
    

    return cell;
    
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, kScreenWidth, kScreenHeight-kViewTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = ColorWithF5F5F5;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}


- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
        self.tableView.mj_header.state = MJRefreshStateIdle;
        self.tableView.mj_footer.state = MJRefreshStateIdle;
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}
- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}

// 新增付款
-(void)clickBtn{
    
    
}

@end
